home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / CollectPictColors / CollectPictColors.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.4 KB  |  226 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CollectPictColors.c
  3.  
  4.     Contains:    This application uses the Picture Utilities package        
  5.                 and Jon Zap's KnowsPict application to demonstrate        
  6.                 two methods of collecting colors used by Pict        
  7.                 resources.  In this program, you'll see different
  8.                 results for each method.  With the Pict Util package,
  9.                 the routine, GetPictInfo, returns a colortable with                    
  10.                 the number of colors requested, but only the colors    
  11.                 used in the picture or it's pixmap(s) image data are
  12.                 stored in the requested colors.  As for the            
  13.                 remaining requested colors not used by the picture    
  14.                 but stored in the picture's pixmap(s) colortable,    
  15.                 they are set to black.  In Jon's application this    
  16.                 doesn't occur.  All the colors used by the picture,    
  17.                 including those stored in the picture's pixmap(s)    
  18.                 colortable are returned.  To use Jon's routines,    
  19.                 simply call CollectColors with the appropriate        
  20.                 parameters.                        
  21.  
  22.     Written by: Edgar Lee            
  23.  
  24.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  25.  
  26.                 You may incorporate this Apple sample source code into your program(s) without
  27.                 restriction. This Apple sample source code has been provided "AS IS" and the
  28.                 responsibility for its operation is yours. You are not permitted to redistribute
  29.                 this Apple sample source code as "Apple sample source code" after having made
  30.                 changes. If you're going to re-distribute the source, we require that you make
  31.                 it clear in the source that the code was descended from Apple sample source
  32.                 code, but that you've made changes.
  33.  
  34.     Change History (most recent first):
  35.                 7/8/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  36.                 02/20/92    Edgar Lee        Created
  37.  
  38. */
  39. /* Constant Declarations */
  40. #include "CLUTBuilder.h"
  41. #include <MacTypes.h>
  42. #include <MacMemory.h>
  43. #include <Quickdraw.h>
  44. #include <Fonts.h>
  45. #include <Windows.h>
  46. #include <Menus.h>
  47. #include <TextEdit.h>
  48. #include <Events.h>
  49. #include <Dialogs.h>
  50. #include <PictUtils.h>
  51. #include <Resources.h>
  52.  
  53.  
  54. #define    WWIDTH        470
  55. #define    WHEIGHT        330
  56.  
  57. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  58. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  59.  
  60. void initMac();
  61. void createWindow();
  62. void drawPictureColors();
  63. void drawColors();
  64. void doEventLoop();
  65.  
  66. void main()
  67. {
  68.     initMac();
  69.     
  70.     createWindow();
  71.     
  72.     doEventLoop();
  73. }
  74.  
  75. void initMac()
  76. {
  77.     MaxApplZone();
  78.  
  79.     InitGraf( &qd.thePort );
  80.     InitFonts();
  81.     InitWindows();
  82.     InitMenus();
  83.     TEInit();
  84.     InitDialogs( nil );
  85.     InitCursor();
  86.     FlushEvents( 0, everyEvent );
  87. }
  88.  
  89. void createWindow()
  90. {
  91.     Rect        rect;
  92.     WindowPtr    window;
  93.     
  94.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  95.     window = NewCWindow( 0L, &rect, "\pCollect Pict Colors", true, documentProc,
  96.                             (WindowPtr)-1L, true, 0L );                        
  97.     SetPort( window );
  98.     
  99.     TextFont( kFontIDGeneva );
  100.     TextSize( 9 );
  101.     TextMode( srcCopy );
  102. }
  103.  
  104. void drawPictureColors()
  105. {
  106.     Rect        rect;
  107.     PicHandle    pict;
  108.     CTabHandle    ctable;
  109.     short        depth, directFlag;
  110.     PictInfo     thePictInfo;
  111.     CTabHandle    CollectColors();
  112.     
  113.     /* Load the pict resource. */
  114.     pict = (PicHandle)GetResource( 'PICT', 128 );
  115.     
  116.     /* Make sure it's not purgeable. */
  117.     HNoPurge( (Handle)pict );
  118.     
  119.     /* Set a black background. */
  120.     SetRect( &rect, 0, 0, WWIDTH, WHEIGHT );
  121.     ForeColor( blackColor );
  122.     PaintRect( &rect );
  123.     
  124.     /* See what CollectColors returns. */
  125.     ctable = CollectColors( pict, &depth, &directFlag );
  126.     drawColors( ctable, 20, "\pCOLLECTCOLORS Colortable" );
  127.     
  128.     /* Draw the picture. */
  129.     rect = (**pict).picFrame;
  130.     OffsetRect( &rect, -rect.left + 160, -rect.top + 70 );
  131.     DrawPicture( pict, &rect );
  132.     
  133.     /* Frame the picture. */
  134.     InsetRect( &rect, -2, -2 );
  135.     FrameRect( &rect );
  136.     
  137.     /* Now see what GetPictInfo returns. */
  138.     GetPictInfo( pict, &thePictInfo, returnColorTable, 256, medianMethod, 0 );
  139.     drawColors( thePictInfo.theColorTable, rect.right + 15, "\pGETPICTINFO Colortable" );
  140.  
  141.     /* Release the resource. */
  142.     ReleaseResource( (Handle)pict );
  143.     
  144.     /* Release the colortable. */
  145.     DisposeCTable( ctable );
  146. }
  147.  
  148. void drawColors( ctable, offset, string )
  149. CTabHandle    ctable;
  150. int            offset;
  151. Str255        string;
  152. {
  153.     int            i;
  154.     int            col, row;
  155.     Rect        rect;
  156.  
  157.     ForeColor( whiteColor );
  158.     BackColor( blackColor );
  159.         
  160.     MoveTo( offset, 20 );
  161.     DrawString( string );
  162.     
  163.     if (ctable != nil)
  164.     {
  165.         for (i = 0; i < 256; i++)
  166.         {
  167.             col = offset + ((i % 8) * 16);
  168.             row = 30 + ((i / 8) * 9);
  169.             
  170.             SetRect( &rect, col, row, col + 10, row + 3 );
  171.             RGBForeColor( &(**ctable).ctTable[i].rgb );
  172.             PaintRect( &rect ); 
  173.             
  174.             ForeColor( whiteColor );
  175.             InsetRect( &rect, -2, -2 );
  176.             FrameRect( &rect );
  177.         }
  178.     }
  179.     else
  180.     {
  181.         MoveTo( 12, offset );
  182.         DrawString( "\p-Sorry could not load PICT resource." );
  183.     }
  184. }
  185.  
  186. void doEventLoop()
  187. {
  188.     EventRecord event;
  189.     WindowPtr   window;
  190.     short       clickArea;
  191.     Rect        screenRect;
  192.  
  193.     for (;;)
  194.     {
  195.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  196.         {
  197.             if (event.what == mouseDown)
  198.             {
  199.                 clickArea = FindWindow( event.where, &window );
  200.                 
  201.                 if (clickArea == inDrag)
  202.                 {
  203.                     screenRect = (**GetGrayRgn()).rgnBBox;
  204.                     DragWindow( window, event.where, &screenRect );
  205.                 }
  206.                 else if (clickArea == inContent)
  207.                 {
  208.                     if (window != FrontWindow())
  209.                         SelectWindow( window );
  210.                 }
  211.                 else if (clickArea == inGoAway)
  212.                     if (TrackGoAway( window, event.where ))
  213.                         return;
  214.             }
  215.             else if (event.what == updateEvt)
  216.             {
  217.                 window = (WindowPtr)event.message;    
  218.                 SetPort( window );
  219.                 
  220.                 BeginUpdate( window );
  221.                 drawPictureColors();
  222.                 EndUpdate( window );
  223.             }
  224.         }
  225.     }
  226. }